<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cis extends CI_Controller {

	function __construct() {
		parent::__construct();
		if(!$this->session->userdata('protocolo')){
			$this->session->sess_destroy();
			redirect('login', 'refresh');
		}
	}
	public function index(){
		if(!$this->session->userdata('protocolo')){
			$this->session->sess_destroy();
			redirect('login', 'refresh');
		}
	
	}
	
	public function novo(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('protocolo');
		temPermissao(array('1','2'),$session_data);
		
		
		$this->load->view('header');
		$this->load->view('view_ci');
		
	}
	
	public function salvar(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('protocolo');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post()){
		
			$contato = new Contato();
			
			$contato->nomecontato = strtoupper($this->input->post("contato"));
			$contato->cargo = strtoupper($this->input->post("cargo"));
			$contato->email = $this->input->post("email");
			$contato->tag = strtoupper($this->input->post("tag"));
			
			$ramal = new Ramal();
			$ramal->where("id",$this->input->post("idramal"))->get();
		
			if($contato->save($ramal)){
				echo 'CONTATO CADASTRADO';
			}else{
				echo $contato->error->string;
			
			}
		
		
		}else{
		
			echo 'ERROR - EMPTY';
		
		}
	
	}
	
	public function editar($contato_id = null, $ramal_id = null){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('protocolo');
		temPermissao(array('1','2'),$session_data);
		
		$contato  = new Contato();
		if($this->input->post()){
			$contato->where("id", $this->input->post("idcontato"))->get();
			if($contato->exists()){
				$contato->contato = strtoupper($this->input->post("contato"));
				$contato->cargo = strtoupper($this->input->post("cargo"));
				$contato->email = $this->input->post("email");
				$contato->tags = strtoupper($this->input->post("tags"));
				if($contato->save()){
					echo 'CONTATO ATUALIZADO';
				}else{
					echo $ramal->error->string;
				}
			}
		}else{
			//CARREGA VIEW_RAMAL_EDITA
			$contato->include_related("ramal")->where("ramais.id",$ramal_id)->where("contatos.id",$contato_id)->get();
			//echo $contato->check_last_query();
			if($contato->exists()){
				$data["contato"] = $contato;
				$this->load->view('header');
				$this->load->view('view_contato_edita',$data);
		
			}else{
				echo "<script>alert('CONTATO NAO POSSUI RAMAL VINCULADO!');</script>";
			}
		}
	}
	
	
	public function consulta(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('protocolo');
		temPermissao(array('1','2'),$session_data);
		
		$contato = new Contato();
		$contato = $contato->consultaContatosAll();
		$data["contatos"] = $contato;
		
		$this->load->view('header');
		$this->load->view('view_contato_consulta', $data);
	
	}
	public function valida(){
	
		$contato = new Contato();
		$contato->include_related('ramal')->where("nomecontato",$this->input->get("contato"))->where('ramais.id',$this->input->get("idramal"))->get();
		
		//echo $contato->check_last_query();
		
		if($contato->exists()){ 
			echo "false";
		}else{ 
			echo "true";
		}
	
	}
	
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */